/**
 * Description:
 * Class Name:ReplacePerson
 * Date:2022/5/3 13:41
 *
 * @author LTisme
 * @version JAVA17
 */
package 疯狂Java讲义.第15章_输入输出._15_8_Java9改进的对象序列化._15_8_5_自定义序列化;

import java.io.ObjectStreamException;
import java.util.ArrayList;

public class ReplacePerson implements java.io.Serializable{
    private String name;
    private int age;

    public ReplacePerson(String name, int age){
        System.out.println("有参数的构造器");
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setName(String name) {
        this.name = name;
    }

    // 重写writeReplace方法，程序在序列化该对象之前，先调用该方法
    private Object writeReplace() throws ObjectStreamException{
        ArrayList<Object> list = new ArrayList<>();
        list.add(name);
        list.add(age);
        return list;
    }
}
